﻿
using System;
using System.Collections.Generic;
using System.Data;

namespace Anno.DynamicApi
{
    /// <summary>
    /// 调用本地DLL
    /// </summary>
    public class LocalDLLExecutor : BaseExecutor
    {
        public LocalDLLExecutor(ApiInfo apiInfo):base(apiInfo, ExecutorType.LocalDLL)
        {
           
        }

        /// <summary>
        /// DLL名称
        /// </summary>
        public string DLLName { get; set; }
        /// <summary>
        /// 类名（需要加命名空间）
        /// </summary>
        public string FullClassName { get; set; }
        /// <summary>
        /// 执行方法
        /// </summary>
        public string Method { get; set; }

        public LocalDLLExecutor(System.Xml.XmlNode xmlNode, ApiInfo apiInfo) : base(xmlNode, apiInfo, ExecutorType.LocalDLL)
        {
            if (xmlNode == null || apiInfo == null || xmlNode.NodeType == System.Xml.XmlNodeType.Comment)
            {
                return;
            }
            Init(xmlNode, apiInfo);
        }
		public LocalDLLExecutor(Newtonsoft.Json.Linq.JToken xmlNode, ApiInfo apiInfo) : base(xmlNode, apiInfo, ExecutorType.LocalDLL)
        {
            if (xmlNode == null || apiInfo == null)
            {
                return;
            }
            Init(xmlNode, apiInfo);
        }
        public override void Init(System.Xml.XmlNode ExecutorNode, ApiInfo apiInfo)
        {
            base.Init(ExecutorNode, apiInfo);
            try
            {
                if (ExecutorNode == null || apiInfo == null || ExecutorNode.NodeType == System.Xml.XmlNodeType.Comment)
                {
                    return;
                }
                Name = ExecutorNode.GetXmlNodeAttributeValue( "Name", Name);
                DLLName = ExecutorNode.GetXmlNodeAttributeValue( "DLLName");
                FullClassName = ExecutorNode.GetXmlNodeAttributeValue( "FullClassName");
                Method = ExecutorNode.GetXmlNodeAttributeValue( "Method", apiInfo.ApiName);

                if (!string.IsNullOrWhiteSpace(DLLName))
                {
                    DLLName = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, DLLName);
                }
            }
            catch { }
        }
        public override void Init(Newtonsoft.Json.Linq.JToken ExecutorNode, ApiInfo apiInfo)
        {
            base.Init(ExecutorNode, apiInfo);
            try
            {
                if (ExecutorNode == null || apiInfo == null)
                {
                    return;
                }
                Name = ExecutorNode.GetXmlNodeAttributeValue("Name", Name);
                DLLName = ExecutorNode.GetXmlNodeAttributeValue("DLLName");
                FullClassName = ExecutorNode.GetXmlNodeAttributeValue("FullClassName");
                Method = ExecutorNode.GetXmlNodeAttributeValue("Method", apiInfo.ApiName);

                if (!string.IsNullOrWhiteSpace(DLLName))
                {
                    DLLName = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, DLLName);
                }
            }
            catch { }
        }
        ///// <summary>
        ///// 返回JSON对象
        ///// </summary>
        //public override object JObject
        //{
        //    get
        //    {
        //        var bJObject = base.JObject as IDictionary<string, object>;

        //        bJObject[nameof(DLLName)] = DLLName;
        //        bJObject[nameof(FullClassName)] = FullClassName;
        //        bJObject[nameof(Method)] = Method;

        //        return bJObject;
        //    }
        //}
        public override IDictionary<string, object> DicAttrs
        {
            get
            {
                var bJObject = base.DicAttrs;

                bJObject[nameof(DLLName)] = DLLName;
                bJObject[nameof(FullClassName)] = FullClassName;
                bJObject[nameof(Method)] = Method;

                return bJObject;
            }
        }
        //---------------------------------------------------------------------------------------------------------------------------------
        protected override object Executor(object input)
        {
            try
            {
                //return WebApi.Models.ApiResult.Success(InvokeLocalHelper.InvokeMethodByEntity(DLLName, FullClassName, Method, input));
                var jsret = InvokeLocalHelper.InvokeMethodByEntity(DLLName, FullClassName, Method, input);
                return getJsonResult(jsret);
            }
            catch (Exception ex)
            {
                return ApiResult.Exception(ex);
                //throw ex;
                //var error =ex.GetExceptionMessage();
                //Console.WriteLine(error);
                //Log.Log.Error(error);
                //return null;
            }
        }
    }
}
